package handerls

import (
	"admin-pro/cache"
	"admin-pro/models"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func HouseAdd(c *gin.Context) {
	houseCode, _ := strconv.Atoi(c.PostForm("houseCode"))
	houseName := c.PostForm("houseName")
	author := c.PostForm("author")
	if houseName == "" || houseCode == 0 || author == "" {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.House{

		HouseCode: houseCode,
		HouseName: houseName,
		Author:    author,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
}
func HouseOrderAdd(c *gin.Context) {
	orderNun, _ := strconv.Atoi(c.PostForm("orderNum"))
	orderPrice, _ := strconv.Atoi(c.PostForm("orderPrice"))
	hid, _ := strconv.Atoi(c.PostForm("hid"))
	if orderPrice == 0 || orderNun == 0 || hid == 0 {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.HouseOrder{

		OrderNun:   orderNun,
		OrderPrice: orderPrice,
		Hid:        hid,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
}

func GetList(c *gin.Context) {
	var arr []models.HouseList
	/*
		SELECT
		house_orders.hid as OrderId,
		houses.house_code as HouseCode,
		houses.house_name as HouseName,
		houses.author as Author,
		house_orders.order_nun as OrderNun,
		house_orders.order_price as OrderPrice
		FROM
		houses
		LEFT JOIN house_orders ON house_orders.hid=houses.id
	*/
	redis_key := "redis_list_"
	redisData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if redisData == "" {
		models.DB.Table("houses").
			Select(
				"house_orders.hid as OrderId",
				"houses.house_code as HouseCode",
				"houses.house_name as HouseName",
				"houses.author as Author",
				"house_orders.order_nun as OrderNun",
				"house_orders.order_price as OrderPrice",
			).
			Joins("LEFT JOIN house_orders ON house_orders.hid=houses.id").Find(&arr)
		jsonData, _ := json.Marshal(arr)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(redisData), &arr)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"list": arr,
	})
}
func GetListDetail(c *gin.Context) {
	id := c.Query("id")
	var arr models.HouseList

	redis_key := "redis_detail_"
	redisData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if redisData == "" {
		models.DB.Table("houses").
			Select(
				"house_orders.hid as OrderId",
				"houses.house_code as HouseCode",
				"houses.house_name as HouseName",
				"houses.author as Author",
				"house_orders.order_nun as OrderNun",
				"house_orders.order_price as OrderPrice",
			).Where("houses.id=?", id).
			Joins("LEFT JOIN house_orders ON house_orders.hid=houses.id").Find(&arr)
		jsonData, _ := json.Marshal(arr)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(redisData), &arr)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"list": arr,
	})
}
